package com.threeonemeeting.tab;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class TabBarButtonDrawable extends Drawable {
	public static int width;
	public static int height;
	private Bitmap bitmap;
	private String mLabel;
	private int mLabelColor;

	public TabBarButtonDrawable(Context context, String label, int labelColor,
			int imageId) {
		mLabel = label;
		mLabelColor = labelColor;
		bitmap = ((BitmapDrawable) context.getResources().getDrawable(imageId))
				.getBitmap();
	}

	public TabBarButtonDrawable(Context context, String label, int labelColor,
			int imageId, int noteImageId) {
		Bitmap srcBm = ((BitmapDrawable) context.getResources().getDrawable(
				imageId)).getBitmap();
		Bitmap coverBm = ((BitmapDrawable) context.getResources().getDrawable(
				noteImageId)).getBitmap();
		if (srcBm == null || coverBm == null)
			return;

		Bitmap.Config config = Bitmap.Config.ARGB_8888;
		bitmap = Bitmap.createBitmap(srcBm.getWidth(), srcBm.getHeight(),
				config);
		Canvas canvas = new Canvas();
		canvas.setBitmap(bitmap);
		canvas.drawBitmap(srcBm, new Matrix(), null);
		float left = srcBm.getWidth() - coverBm.getWidth();
		float top = 0;
		canvas.drawBitmap(coverBm, left, top, null);
		mLabel = label;
		mLabelColor = labelColor;
	}

	@Override
	public void draw(Canvas canvas) {
		int bwidth = bitmap.getScaledWidth(canvas);
		int bheight = bitmap.getScaledHeight(canvas);
		int textSize = 14;
		int textMarginTop = 12;
		boolean noLabel = false;

		if (mLabel == null || mLabel.equals("") == true) {
			textSize = 0;
			textMarginTop = 0;
			noLabel = true;
		}

		int x = (width - bwidth) / 2;
		int y = (height - (bheight + textSize + textMarginTop)) / 2;

		canvas.drawColor(Color.TRANSPARENT);

		if (!noLabel) {
			Paint p = new Paint();
			p.setColor(mLabelColor);
			p.setStyle(Paint.Style.FILL);
			p.setTextSize(textSize);
			p.setTypeface(Typeface.DEFAULT_BOLD);
			p.setFakeBoldText(true);
			p.setTextAlign(Align.CENTER);
			// p.setShader(textShader);
			p.setAntiAlias(true);
			canvas.drawText(mLabel, width / 2, y + bheight + textMarginTop, p);
		}
		canvas.drawBitmap(bitmap, x, y, null);
	}

	@Override
	public int getOpacity() {
		return 0;
	}

	@Override
	public void setAlpha(int alpha) {

	}

	@Override
	public void setColorFilter(ColorFilter cf) {

	}
}
